<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>品牌首页</title>
    <style>
        /* 默认样式 */
        a {
            color: black; /* 默认黑色 */
            text-decoration: none; /* 默认无下划线 */
        }
        /* 鼠标移上去后的样式 */
        a:hover {
            color: red; /* 移上去后为红色 */
        }
    </style>
</head>
<body>
<a href="addBrand.jsp"><input type="button" value="新增"></a><br>
<hr>
<table id="brandTable" border="1" cellspacing="0" width="100%">
    <tr>
        <th>序号</th>
        <th>品牌名称</th>
        <th>企业名称</th>
        <th>排序</th>
        <th>品牌介绍</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
</table>
</body>
</html>

<script src="js/jquery-3.7.1.js"></script>
    <script>
        $(function() {
            // 页面加载完成后执行的代码
            selectBrand()


        });


        function selectBrand() {
            // 清空表格内容
            $("#brandTable tr:not(:first)").remove();
            $.ajax({
                url:"selectBrandServlet",
                type:"GET",
                success:function (data) {
                    console.log(data)
                    //JSON字符串转换为JSON对象
                    var brand = JSON.parse(data);
                    for (let i = 0; i <brand.length; i++) {
                        console.log(brand[i])
                        // 更新表格数据
                        var newRow = "<tr align='center'>" +
                            "<td>" + brand[i].id + "</td>" +
                            "<td>" + brand[i].brandName + "</td>" +
                            "<td>" + brand[i].companyName + "</td>" +
                            "<td>" + brand[i].ordered + "</td>" +
                            "<td>" + brand[i].description + "</td>" +
                            "<td>" + brand[i].status + "</td>" +
                            "<td><a href='updateBrand.jsp?id=" + brand[i].id + "'>修改</a> <a href='#' onclick='deleteBrand(" + brand[i].id + ")'>删除</a></td>"+
                            "</tr>";
                        $("#brandTable").append(newRow);
                    }
                }
            })
        }

        function deleteBrand(id) {
            console.log(id)
            $.ajax({
                url: "deleteBrandServlet?id="+id,
                type:"GET",
                success:function (data) {
                    selectBrand()
                    alert(data)
                }
            })
        }

    </script>

